﻿using UnityEngine;

public class RotationSystem
{
    public static void Action(Entity entity)
    {
        if (entity.position!=null && entity.rotationMovement!=null){
            if (entity.rotationMovement.turn == false)
            {
                return;
            }
            if (entity.rotationMovement.right){
                entity.position.rotaion += entity.rotationMovement.rotaionSpeed;
            }else{
                entity.position.rotaion -= entity.rotationMovement.rotaionSpeed;
            }
            while (entity.position.rotaion < 0)
            {
                entity.position.rotaion += 360;
            }
            entity.position.update();
        }
        else  if (entity.position!=null && entity.targetRotationMovement!=null){
            if (entity.targetRotationMovement.turn == false)
            {
                return;
            }

            if (entity.position.rotaion == entity.targetRotationMovement.wantRotaion)
            {
                return;
            }

            //Debug.Log("wantRotaion: " + entity.targetRotationMovement.wantRotaion);
            //Debug.Log("Rotaion: " + entity.position.rotaion);

            if (Mathf.Abs((float)(entity.targetRotationMovement.wantRotaion - entity.position.rotaion)) <= (float)(entity.targetRotationMovement.rotaionSpeed))
            {
                entity.position.rotaion = entity.targetRotationMovement.wantRotaion;
                //GameObject.Find("Canvas/Text").GetComponent<Text>().text = "wantRotaion: " + entity.targetRotationMovement.wantRotaion;
            }
            else
            {
                if (entity.targetRotationMovement.right)
                {
                    entity.position.rotaion += entity.targetRotationMovement.rotaionSpeed;
                }
                else
                {
                    entity.position.rotaion -= entity.targetRotationMovement.rotaionSpeed;
                }
            }
            entity.position.update();
        }
    }
}
